;(function($){
	
	var buttonList = function($element, config) {
		config = config || {};
		
		defaults = buttonList.defaultConfig;
		
		// 合并默认配置
	    for (var i in defaults) {
	        if (config[i] === undefined) {
	            config[i] = defaults[i];
	        };
	    };
		
		config = $.extend(buttonList.defaultConfig, config);
		return new buttonList.fn.constructor($element, config);
	}
	
	buttonList.fn = buttonList.prototype = {
		constructor: function ($element, config) {
			this.$element = $element;
			this.config = config;
			this.hidden = $('#'+config.hiddenId);
			this.init();
		},
	
		init: function() {
			var $element = this.$element,
				config = this.config,
				hidden = this.hidden,
				$children = $element.children();
			
			$children.click(function(event) {
				$ele = $(this);
				$children.removeClass(config.itemCurrentClass);
				$ele.addClass(config.itemCurrentClass);
				key = $ele.attr('key');
				hiddenName = hidden.attr('name');
				$form = $ele.closest('form');
				$('[name="'+hiddenName+'"]', $form).not(':first').remove().end().val(key);
				if(config.submitOnClick) {
					if($form) $form.submit();
				}
			})
		}
	}
	
	buttonList.fn.constructor.prototype = buttonList.fn;
	$.fn.buttonList = function(config) {
		return buttonList(this, config);
	}
	
	buttonList.defaultConfig = {
		/**
		 * 选择后立即提交表单
		 */
		submitOnClick: true	
	}
	
})(jQuery);